package me.zhengjie.modules.service.impl;

import cn.hutool.core.date.DateTime;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.domain.Api;
import me.zhengjie.modules.domain.ApiBatch;
import me.zhengjie.modules.domain.Batch;
import me.zhengjie.modules.domain.Companies;
import me.zhengjie.modules.domain.enterprise.EnterpriseAbstract;
import me.zhengjie.modules.domain.enterprise.EnterpriseInformation;
import me.zhengjie.modules.domain.enterprise.EnterpriseTax;
import me.zhengjie.modules.repository.ApiBatchMapper;
import me.zhengjie.modules.repository.ApiMapper;
import me.zhengjie.modules.repository.BatchMapper;
import me.zhengjie.modules.repository.CompaniesMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseInfMapper;
import me.zhengjie.modules.service.EnterpriseAbsService;
import me.zhengjie.modules.repository.enterprise.EnterpriseAbstractMapper;
import me.zhengjie.modules.utils.Contents;
import me.zhengjie.modules.utils.JsonUtils;
import me.zhengjie.modules.utils.MyHttpUtils;
import net.dreamlu.mica.core.utils.JsonUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("EnterpriseAbsService")
@RequiredArgsConstructor
public class EnterpriseAbsServiceImpl implements EnterpriseAbsService {

    private final EnterpriseAbstractMapper abstractMapper;
    private final CompaniesMapper companiesMapper;

    private final ApiMapper apiMapper;
    private final BatchMapper batchMapper;
    private final ApiBatchMapper apiBatchMapper;

    @Override
    public int addEnterpriseAbs(EnterpriseAbstract enterpriseAbstract) {
        return abstractMapper.insertEnterpriseAbs(enterpriseAbstract);
    }

    //@Scheduled(fixedDelay = 1000*24*60*60,initialDelay=1000*60*2)
    @Transactional
    public void AutoAddEnterpriseAbs(){
        Api api=apiMapper.selectApiByVariableName("BASIC_ENTERPRISE_ABS");
        ApiBatch apiBatches=apiBatchMapper.selectApiBatchByApiId(api.getId());
        if(apiBatches!=null){
            //apiBatches不为空的情况，说明有查到数据，在这个基础上判断是否满足执行周期
            //满足执行周期则重新获取一次
            //不满足则跳过 结束方法
            Date now=new Date();
            long poor=now.getTime()-apiBatches.getCreateTime().getTime();
            System.out.println(poor);
            long day= (long)api.getCycle()*24*60*60*1000;
            System.out.println(day);
            if(poor>day){
                //到执行周期了
                System.out.println("执行新一轮的企业简介信息数据添加");
                Long batchId= util.SnowFlakeUtil.getSnowFlakeId();
                //从数据库中查询最新的一批企业基本信息 进行关联
                List<Companies> companiesList=companiesMapper.selectAll();
                if(companiesList!=null&&companiesList.size()>0){
                    for (Companies c : companiesList) {
                        EnterpriseAbstract abs=JsonUtils.JsonToEnterpriseAbs(MyHttpUtils.GetJson(Contents.token,"http://open.api.tianyancha.com/services/v4/open/profile?keyword="+c.getCompanyName()));
                        if(abs!=null){
                            abs.setBatchId(batchId);
                            abs.setCompaniesId(c.getId());
                            abstractMapper.AutoInsertEnterpriseAbs(abs);
                        }
                    }
                    Batch batch=new Batch();
                    batch.setBatchNo(batchId);
                    batch.setCreateTime(new DateTime());
                    batchMapper.insertBatch(batch);
                    ApiBatch apiBatch=new ApiBatch();
                    apiBatch.setApiId(api.getId());
                    apiBatch.setBatchId(batchId);
                    apiBatch.setCreateTime(new DateTime());
                    apiBatchMapper.insertApiBatch(apiBatch);
                }
            }else{
                //执行周期没到，直接结束方法运行
                return;
            }
        }else{
            //apiBatches为空的情况 说明这个数据集是第一次执行，数据库中没有数据
            //直接从数据库中获取最新的企业基本信息  作为条件查询
            //这种情况要从天眼查获取第一轮数据
            System.out.println("暂无数据");
            Long batchId= util.SnowFlakeUtil.getSnowFlakeId();
            //从数据库中查询最新的一批企业基本信息 进行关联
            List<Companies> companiesList=companiesMapper.selectAll();
            if(companiesList!=null&&companiesList.size()>0){
                for (Companies c : companiesList) {
                    EnterpriseAbstract abs=JsonUtils.JsonToEnterpriseAbs(MyHttpUtils.GetJson(Contents.token,"http://open.api.tianyancha.com/services/v4/open/profile?keyword="+c.getCompanyName()));
                    if(abs!=null){
                        abs.setBatchId(batchId);
                        abs.setCompaniesId(c.getId());
                        abstractMapper.AutoInsertEnterpriseAbs(abs);
                    }
                }
                Batch batch=new Batch();
                batch.setBatchNo(batchId);
                batch.setCreateTime(new DateTime());
                batchMapper.insertBatch(batch);
                ApiBatch apiBatch=new ApiBatch();
                apiBatch.setApiId(api.getId());
                apiBatch.setBatchId(batchId);
                apiBatch.setCreateTime(new DateTime());
                apiBatchMapper.insertApiBatch(apiBatch);
            }
        }
    }

    @Override
    public EnterpriseAbstract queryEnterpriseAbsByName(String name) {
        return abstractMapper.selectEnterpriseAbsByName(name);
    }

    @Override
    public EnterpriseAbstract queryEnterpriseAbsById(Long id) {
        return abstractMapper.selectEnterpriseAbsById(id);
    }
}
